<?php

namespace app\channel\controller\api;
use app\channel\ApiController;

use app\channel\model\ProxyUsersModel;
use app\channel\model\ProxyLevelModel;
use app\channel\model\ProxyUpapplyLogModel;
/*------------------------------------------------------ */
//-- 代理会员相关API
/*------------------------------------------------------ */

class ProxyUsers extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行  当前由继承Users，$this->userInfo为会员
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new ProxyUsersModel();
    }
    /*------------------------------------------------------ */
    //-- 获取登陆代理信息
    /*------------------------------------------------------ */
    public function getInfo()
    {
        $data['proxyInfo'] = $this->Model->info($this->userInfo['user_id']);
        //判断代理满足加入门槛
        $data['proxyInfo'] = $this->Model->joinCondition($data['proxyInfo']);
        return $this->success($data);
    }
    /*------------------------------------------------------ */
    //-- 获取登陆代理上级信息
    /*------------------------------------------------------ */
    public function getSuperiorInfo()
    {
        $proxyInfo = $this->Model->info($this->userInfo['user_id']);
        //推荐上级
        if ($proxyInfo['pid'] == 0){
            $data['parent_name'] = '厂家';
            $data['parent_level'] = '厂家';
            $data['parent_headimgurl'] = settings('logo');
            $data['parent_mobile'] = settings('channel_mobile');
            $data['parent_warrant_end_time'] = '--';
            $data['parent_reg_time'] = '--';
        }else{
            $parentUser = $this->Model->info($proxyInfo['pid']);
            $data['parent_name'] = $parentUser['byUser']['real_name'];
            $data['parent_headimgurl'] = $parentUser['byUser']['headimgurl'];
            $data['parent_mobile'] = $parentUser['byUser']['mobile'];
            $data['parent_level'] = $parentUser['proxyLevel']['proxy_name'];
            $data['parent_warrant_end_time'] = dateTpl($parentUser['warrant_end_time'],'Y-m-d H:i:s',true);
            $data['parent_reg_time'] = dateTpl($parentUser['reg_time'],'Y-m-d H:i:s',true);
        }
        //供货上级
        if ($this->userInfo['supply_uid'] == 0){
            $data['supply_name'] = '厂家';
            $data['supply_level'] = '厂家';
            $data['supply_headimgurl'] = settings('logo');
            $data['supply_mobile'] = settings('channel_mobile');
            $data['supply_warrant_end_time'] = '--';
            $data['supply_reg_time'] = '--';
        }else{
            $supplytUser = $this->Model->info($this->userInfo['supply_uid']);
            $data['supply_name'] = $supplytUser['byUser']['real_name'];
            $data['supply_headimgurl'] = $supplytUser['byUser']['headimgurl'];
            $data['supply_mobile'] = $supplytUser['byUser']['mobile'];
            $data['supply_level'] = $supplytUser['proxyLevel']['proxy_name'];
            $data['supply_warrant_end_time'] = dateTpl($supplytUser['warrant_end_time'],'Y-m-d H:i:s',true);
            $data['supply_reg_time'] = dateTpl($supplytUser['reg_time'],'Y-m-d H:i:s',true);
        }
        return $this->success($data);
    }



    /*------------------------------------------------------ */
    //-- 获取可邀请的代理层级
    /*------------------------------------------------------ */
    public function getInvitePorxyLevel(){
        $proxy_id = $this->Model->where('user_id',$this->userInfo['user_id'])->value('proxy_id');
        $data['proxyLevelList'] = (new ProxyLevelModel)->getInvitelist($proxy_id);
        return $this->success($data);
    }

    /*------------------------------------------------------ */
    //-- 获取邀请海报
    /*------------------------------------------------------ */
    public function getInvitePoster(){
        $proxy_id = input('proxy_id',0,'intval');
        $res = $this->Model->getInvitePoster($this->userInfo['user_id'],$proxy_id);
        if (is_array($res) == false){
            return $this->error($res);
        }
        return $this->success($res);
    }
    /*------------------------------------------------------ */
    //-- 获取可升级的层级
    /*------------------------------------------------------ */
    public function getUpgradeLevel(){
        $proxy_id = $this->Model->where('user_id',$this->userInfo['user_id'])->value('proxy_id');
        $data = (new ProxyLevelModel)->getUpgradeLevel($this->userInfo['user_id'],$proxy_id);
        return $this->success($data);
    }
    /*------------------------------------------------------ */
    //-- 升级申请处理
    /*------------------------------------------------------ */
    public function appUpgrade(){
        $proxy_id = input('proxy_id','0','intval');
        if ($proxy_id < 1){
            return $this->error('传值错误.');
        }
        $ProxyLevelModel = new ProxyLevelModel();
        $res = $ProxyLevelModel->checkUpgrade($this->userInfo['user_id'],$proxy_id);
        if ($res !== true){
            return $this->error($res);
        }
        $proxyLevel = $ProxyLevelModel->info($proxy_id);
        if ($proxyLevel['is_auto_check'] == 0){ //非自动审核，写入申请记录
            $ProxyUpapplyLogModel = new ProxyUpapplyLogModel();
            $where[] = ['user_id','=',$this->userInfo['user_ud']];
            $where[] = ['status','=',0];
            $count = $ProxyUpapplyLogModel->where($where)->count();
            if ($count > 0){
                return $this->error('您已提交升级申请，正在审核中，不能重复提交.');
            }
            $ProxyUsersModel = new ProxyUsersModel();
            $inData['user_id'] = $this->userInfo['user_id'];
            $inData['proxy_id'] = $this->userInfo['proxy_id'];
            $inData['up_proxy_id'] = $proxy_id;
            $inData['pid'] = $this->userInfo['pid'];
            if ($inData['pid'] > 0){
                $pUserInfo = $ProxyUsersModel->info($inData['pid']);
                $inData['pid_proxy_id'] = $pUserInfo['proxy_id'];
            }
            $inData['supply_uid'] = $this->userInfo['supply_uid'];
            if ($inData['supply_uid'] > 0){
                $sUserInfo = $ProxyUsersModel->info($inData['pid']);
                $inData['supply_proxy_id'] = $sUserInfo['proxy_id'];
            }
            $inData['add_time'] = time();
            $res = $ProxyUpapplyLogModel->save($inData);
            if ($res < 1){
                return $this->error('录入升级申请失败，请重试.');
            }
            $msg = '提交成功，等待审核.';
        }else{//执行升级
            $res = $this->Model->upgrade($this->userInfo['user_id'],$proxy_id,'自动审核通过完成升级',$this);
            if ($res !== true){
                return $this->error($res);
            }
            $msg = '升级成功.';
        }
        return $this->success($msg);
    }
}
